﻿<%@ Page Title="" Language="C#" MasterPageFile="~/TsSite.Master" AutoEventWireup="true" CodeBehind="QuotationCalculator.aspx.cs" Inherits="TireService.Web.View.SalesView.QuotationCalculator" %>

<%@ Register Assembly="DevExpress.Web.v11.1, Version=11.1.12.0, Culture=neutral, PublicKeyToken=b88d1754d700e49a" Namespace="DevExpress.Web.ASPxCallback" TagPrefix="dx" %>
<%@ Register Assembly="DevExpress.Web.v11.1, Version=11.1.12.0, Culture=neutral, PublicKeyToken=b88d1754d700e49a" Namespace="DevExpress.Web.ASPxPopupControl" TagPrefix="dx" %>
<%@ Register assembly="DevExpress.Web.v11.1, Version=11.1.12.0, Culture=neutral, PublicKeyToken=b88d1754d700e49a" namespace="DevExpress.Web.ASPxPanel" tagprefix="dx" %>
<%@ Register Assembly="DevExpress.Web.v11.1, Version=11.1.12.0, Culture=neutral, PublicKeyToken=b88d1754d700e49a" Namespace="DevExpress.Web.ASPxRoundPanel" TagPrefix="dx" %>
<%@ Register Assembly="DevExpress.Web.ASPxGridView.v11.1.Export, Version=11.1.12.0, Culture=neutral, PublicKeyToken=b88d1754d700e49a" Namespace="DevExpress.Web.ASPxGridView.Export" TagPrefix="dx" %>
<%@ Register assembly="DevExpress.Web.ASPxGridView.v11.1, Version=11.1.12.0, Culture=neutral, PublicKeyToken=b88d1754d700e49a" namespace="DevExpress.Web.ASPxGridView" tagprefix="dx" %>
<%@ Register assembly="DevExpress.Web.ASPxEditors.v11.1, Version=11.1.12.0, Culture=neutral, PublicKeyToken=b88d1754d700e49a" namespace="DevExpress.Web.ASPxEditors" tagprefix="dx" %>
<%@ Register Assembly="DevExpress.Web.v11.1, Version=11.1.12.0, Culture=neutral, PublicKeyToken=b88d1754d700e49a" Namespace="DevExpress.Web.ASPxLoadingPanel" TagPrefix="dx" %>

<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="server">
</asp:Content>

<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">

    <table cellpadding="0" cellspacing="2" border="0">
        <tr>
            <td>
                <img id="img1" alt="" src="~/Images/Medium/money.png" runat="server" style="width: 32px; height: 32px" />
            </td>
            <td valign="middle">
                <h3><asp:Label ID="lblQuotationCalculator" runat="server" Text="Opret tilbud"></asp:Label></h3>
            </td>
        </tr>
    </table>
    <hr />

    <!-- Javascript of action dropdown list -->
    <script language="javascript" type="text/javascript">

        // Function that calculates and show quotation key figures
        function OnVendorPricesGridFocusedRowChanged() {
            gvVendorPrices.GetRowValues(gvVendorPrices.GetFocusedRowIndex(), 'TirePriceListId;Description;CostPrice', CalculateQuotationPrice);
        }

        function CalculateQuotationPrice(values) {
            if (values == null) return;
            if (values.length == 0) return;

            // Calculate quotation
            lblTirePriceListId.SetText(values[0]);
            lblSelectedTire.SetText(values[1]);

            var costprice = parseFloat(values[2]);
            var tireprice = parseFloat(costprice) + parseFloat(txtProfitMargin.GetText());
            var mountingprice = parseFloat(txtMountingPrice.GetText());
            var salesprice = parseFloat(tireprice + mountingprice);
            var profit = parseFloat(salesprice - costprice);

            lblCostPrice.SetText(costprice);
            lblTirePrice.SetText(tireprice);
            lblMountingPrice.SetText(mountingprice);

            lblTotalSalesPrice.SetText(salesprice);
            lblProfit.SetText(profit);

            // Show totals
            var tirequantity = parseFloat(gvTargetCustomers.cpReplaceQuantitySummary);
            var totalprofit = parseFloat(tirequantity * profit);

            lblTireQuantity.SetText(tirequantity);
            lblTireProfit.SetText(totalprofit);
            
            // Show UI
            pQuotationCalculation.SetVisible(true);
        }

        function SendQuotationToCustomer() {
            LoadingPanel.Show();

//            int TireSetId = Convert.ToInt32(Request.QueryString["TireSetId"]);
//            int VF = Convert.ToInt32(Request.QueryString["VF"]);
//            int HF = Convert.ToInt32(Request.QueryString["HF"]);
//            int VB = Convert.ToInt32(Request.QueryString["VB"]);
//            int HB = Convert.ToInt32(Request.QueryString["HB"]);
//            int TireTypeId = Convert.ToInt32(Request.QueryString["TireType"]);
//            int TargetGroupType = Convert.ToInt32(Request.QueryString["TargetGroupType"]);
//            bool Business = Convert.ToBoolean(Convert.ToByte(Request.QueryString["Business"]));
//            decimal CostPrice = 0;
//            decimal SalesPrice = 0;
//            DateTime ExpireDate;

            var input = lblTirePriceListId.GetText() + '|';
            input += txtMountingPrice.GetText() + '|';
            input += txtProfitMargin.GetText() + '|';
            input += txtExpireDate.GetText() + '|';



            callBackSendQuotationToCustomer.PerformCallback(txtExpireDate.GetText());
        }

        function OnCallbackSendQuotationToCustomerComplete(s, e) {

            LoadingPanel.Hide();
        }

        function CallbackError() {
            alert('Fejl opstod - prøv venligst igen');
        }

    </script>

    <dx:ASPxLoadingPanel ID="LoadingPanel" Text="Vent venligst..." ClientInstanceName="LoadingPanel" runat="server" Modal="True">
    </dx:ASPxLoadingPanel>

    <dx:ASPxCallback ID="callBackSendQuotationToCustomer" ClientInstanceName="callBackSendQuotationToCustomer" runat="server" OnCallback="callBackSendQuotationToCustomer_Callback">
        <ClientSideEvents CallbackError="CallbackError" CallbackComplete="OnCallbackSendQuotationToCustomerComplete" />
    </dx:ASPxCallback>

    <h4>Beregn tilbud</h4>
    <table width="100%">
        <tr>
            <td valign="top">
                <b>Dæk data</b>
                <table>
                    <tr>
                        <td>Type</td>
                        <td>
                            <dx:ASPxLabel ID="lblType" ClientInstanceName="lblType" runat="server" Text="">
                            </dx:ASPxLabel>
                        </td>
                    </tr>
                    <tr>
                        <td>Dimensioner</td>
                        <td>
                            <dx:ASPxLabel ID="lblDimensions" ClientInstanceName="lblDimensions" runat="server" Text="">
                            </dx:ASPxLabel>
                        </td>
                    </tr>
                    <tr>
                        <td>Loadindex</td>
                        <td>
                            <dx:ASPxLabel ID="lblLoadIndex" ClientInstanceName="lblLoadIndex" runat="server" Text="">
                            </dx:ASPxLabel>
                        </td>
                    </tr>
                    <tr>
                        <td>Hastighedsindex</td>
                        <td>
                            <dx:ASPxLabel ID="lblSpeedIndex" ClientInstanceName="lblSpeedIndex" runat="server" Text="">
                            </dx:ASPxLabel>
                        </td>
                    </tr>
                </table>
            </td>
            <td valign="top">
                <b>Avance data (pr. stk)</b><br />
                <table>
                    <tr>
                        <td>Dæk avance</td>
                        <td>
                            <dx:ASPxTextBox ID="txtProfitMargin" ClientInstanceName="txtProfitMargin" runat="server" Width="50px" Text="125">
                            </dx:ASPxTextBox>
                        </td>
                    </tr>
                    <tr>
                        <td>Monteringspris</td>
                        <td>
                            <dx:ASPxTextBox ID="txtMountingPrice" ClientInstanceName="txtMountingPrice" runat="server" Width="50px" Text="135">
                            </dx:ASPxTextBox>
                        </td>
                    </tr>
                    <tr>
                        <td colspan="2">
                            <dx:ASPxButton ID="btnRecalculateQuotationPrice" ClientInstanceName="btnRecalculateQuotationPrice" runat="server" Text="Opdater tilbud" AutoPostBack="false">
                                <ClientSideEvents Click="function(s, e) { OnVendorPricesGridFocusedRowChanged();}" />
                            </dx:ASPxButton>
                        </td>
                    </tr>
                </table>
            </td>
        </tr>
    </table>
    <hr />
    <table width="100%">
        <tr>
            <td valign="top">
                <!-- Show selected customers -->
                <b>Kunder/biler valgt</b>
                <dx:ASPxGridView ID="gvTargetCustomers" ClientInstanceName="gvTargetCustomers" runat="server" 
                    KeyFieldName="TireSetId" AutoGenerateColumns="false" EnableCallBacks="True" OnCustomCallback="gvTargetCustomers_CustomCallback"
                    OnCustomUnboundColumnData="gvTargetCustomers_CustomUnboundColumnData" OnDataBound="gvTargetCustomers_DataBound">
                    <SettingsLoadingPanel Text="Henter&amp;hellip;" />
                    <Settings ShowHeaderFilterButton="true" ShowGroupPanel="true" ShowFilterRow="True" ShowFooter="True"  />
                    <SettingsPager AlwaysShowPager="False" PageSize="10" Position="Bottom">
                        <Summary 
                            AllPagesText="Sider: {0} - {1} ({2} kunder)" 
                            Text="Side {0} af {1} ({2} kunder)" />
                    </SettingsPager>
                    <SettingsText ConfirmDelete="" GroupPanel="Træk kolonne hertil for at gruppere efter den" HeaderFilterShowAll="Vis alle" />
                    <SettingsBehavior ConfirmDelete="false" AllowGroup="false" AllowDragDrop="false" AllowFocusedRow="true" />                    
                    <Columns>
                        <dx:GridViewDataColumn FieldName="TireSetId" VisibleIndex="1" Caption="Hjulsæt Id">
                        </dx:GridViewDataColumn>
                        <dx:GridViewDataColumn FieldName="Name" VisibleIndex="1" Caption="Navn">
                        </dx:GridViewDataColumn>
                        <dx:GridViewDataColumn FieldName="CustomerId" VisibleIndex="2" Caption="Kundenr">
                        </dx:GridViewDataColumn>
                        <dx:GridViewDataColumn FieldName="RegistrationNumber" VisibleIndex="2" Caption="Regnr">
                        </dx:GridViewDataColumn>
                        <dx:GridViewDataColumn FieldName="LeftFrontTirePattern" VisibleIndex="3" Caption="VF">
                        </dx:GridViewDataColumn>
                        <dx:GridViewDataColumn FieldName="RightFrontTirePattern" VisibleIndex="3" Caption="HF">
                        </dx:GridViewDataColumn>
                        <dx:GridViewDataColumn FieldName="LeftRearTirePattern" VisibleIndex="3" Caption="VB">
                        </dx:GridViewDataColumn>
                        <dx:GridViewDataColumn FieldName="RightRearTirePattern" VisibleIndex="3" Caption="HB">
                        </dx:GridViewDataColumn>
                        <dx:GridViewDataTextColumn FieldName="ReplaceQuantity" VisibleIndex="3" UnboundType="Integer" Caption="Antal dæk">
                        </dx:GridViewDataTextColumn>
                        <dx:GridViewDataColumn VisibleIndex="3">
                            <DataItemTemplate>
                                <dx:ASPxButton ID="btnRemoveCustomer" runat="server" ClientInstanceName="btnRemoveCustomer" Text="Fjern" ToolTip="Klik for at fjerne kunden i listen over mål-/modtagergruppe" AutoPostBack="false"
                                    ClientSideEvents-Click='<%# "function(s, e) { RemoveCustomer(" + Container.KeyValue + "); }" %>'>
                                </dx:ASPxButton>                                           
                            </DataItemTemplate>
                        </dx:GridViewDataColumn>
                    </Columns>
                    <TotalSummary>
                        <dx:ASPxSummaryItem FieldName="ReplaceQuantity" SummaryType="Sum" />
                    </TotalSummary>
                </dx:ASPxGridView>
            </td>
            <td valign="top">
                <!-- Show possible tires for quotation -->
                <b>Dæk og leverandør priser</b>
                <dx:ASPxGridView ID="gvVendorPrices" ClientInstanceName="gvVendorPrices" runat="server" 
                    KeyFieldName="TirePriceListId" AutoGenerateColumns="false" EnableCallBacks="True" OnCustomCallback="gvVendorPrices_CustomCallback">
                    <SettingsLoadingPanel Text="Henter&amp;hellip;" />
                    <Settings ShowHeaderFilterButton="true" ShowGroupPanel="true" ShowFilterRow="True" ShowFooter="True"  />
                    <SettingsPager AlwaysShowPager="False" PageSize="10" Position="Bottom">
                        <Summary 
                            AllPagesText="Sider: {0} - {1} ({2} dæk)" 
                            Text="Side {0} af {1} ({2} dæk)" />
                    </SettingsPager>
                    <SettingsText ConfirmDelete="" GroupPanel="Træk kolonne hertil for at gruppere efter den" HeaderFilterShowAll="Vis alle" />
                    <SettingsBehavior ConfirmDelete="false" AllowGroup="false" AllowDragDrop="false" AllowFocusedRow="true" />
                    <ClientSideEvents FocusedRowChanged="function(s, e) { OnVendorPricesGridFocusedRowChanged();}" />
                    <Columns>
                        <dx:GridViewDataColumn FieldName="TirePriceListId" VisibleIndex="0" Visible="false" Caption="Id">
                        </dx:GridViewDataColumn>
                        <dx:GridViewDataColumn FieldName="Description" VisibleIndex="1" Caption="Beskrivelse">
                        </dx:GridViewDataColumn>
                        <dx:GridViewDataColumn FieldName="Model" VisibleIndex="1" Caption="Model">
                        </dx:GridViewDataColumn>
                        <dx:GridViewDataColumn FieldName="CostPrice" VisibleIndex="2" Caption="Kostpris">
                        </dx:GridViewDataColumn>
                        <dx:GridViewDataColumn FieldName="StockQuantity" VisibleIndex="2" Caption="Lager">
                        </dx:GridViewDataColumn>
                    </Columns>
                </dx:ASPxGridView>
            </td>
            <td valign="top">
                <b>Beregn og udsend tilbud</b>
                <!-- Calculate quotation based on selected tire -->
                <dx:ASPxPanel ID="pQuotationCalculation" ClientInstanceName="pQuotationCalculation" runat="server" Width="100%" ClientVisible="false">
                    <PanelCollection>
                        <dx:PanelContent>
                            <table>
                                <tr>
                                    <td>
                                        Levendør pris ID
                                    </td>
                                    <td>
                                        <dx:ASPxLabel ID="lblTirePriceListId" ClientInstanceName="lblTirePriceListId" runat="server" Text="">
                                        </dx:ASPxLabel>
                                    </td>
                                </tr>
                                <tr>
                                    <td>
                                        Valgt dæk
                                    </td>
                                    <td>
                                        <dx:ASPxLabel ID="lblSelectedTire" ClientInstanceName="lblSelectedTire" runat="server" Text="">
                                        </dx:ASPxLabel>
                                    </td>
                                </tr>
                                <tr>
                                    <td colspan="2">
                                        <hr />
                                    </td>
                                </tr>
                                <tr>
                                    <td>
                                        Kostpris (ex. moms)
                                    </td>
                                    <td>
                                        <dx:ASPxLabel ID="lblCostPrice" ClientInstanceName="lblCostPrice" runat="server" Text="">
                                        </dx:ASPxLabel>
                                    </td>
                                </tr>
                                <tr>
                                    <td>
                                        Dæk pris
                                    </td>
                                    <td>
                                        <dx:ASPxLabel ID="lblTirePrice" ClientInstanceName="lblTirePrice" runat="server" Text="">
                                        </dx:ASPxLabel>
                                    </td>
                                </tr>
                                <tr>
                                    <td>
                                        Monteringspris
                                    </td>
                                    <td>
                                        <dx:ASPxLabel ID="lblMountingPrice" ClientInstanceName="lblMountingPrice" runat="server" Text="">
                                        </dx:ASPxLabel>
                                    </td>
                                </tr>
                                <tr>
                                    <td colspan="2">
                                        <hr />
                                    </td>
                                </tr>
                                <tr>
                                    <td>
                                        Total salgspris
                                    </td>
                                    <td>
                                        <dx:ASPxLabel ID="lblTotalSalesPrice" ClientInstanceName="lblTotalSalesPrice" runat="server" Text="">
                                        </dx:ASPxLabel>
                                    </td>
                                </tr>
                                <tr>
                                    <td>
                                        Dækningsbidrag
                                    </td>
                                    <td>
                                        <dx:ASPxLabel ID="lblProfit" ClientInstanceName="lblProfit" runat="server" Text="">
                                        </dx:ASPxLabel>
                                    </td>
                                </tr>
                                <tr>
                                    <td colspan="2">
                                    <hr />
                                    </td>
                                </tr>
                                <tr>
                                    <td>
                                        Total antal dæk
                                    </td>
                                    <td>
                                        <dx:ASPxLabel ID="lblTireQuantity" ClientInstanceName="lblTireQuantity" runat="server" Text="">
                                        </dx:ASPxLabel>
                                    </td>
                                </tr>
                                <tr>
                                    <td>
                                        Total DB
                                    </td>
                                    <td>
                                        <dx:ASPxLabel ID="lblTireProfit" ClientInstanceName="lblTireProfit" runat="server" Text="">
                                        </dx:ASPxLabel>
                                    </td>
                                </tr>
                                <tr>
                                    <td>
                                        Tilbud udløbsdato
                                    </td>
                                    <td>
                                        <dx:ASPxDateEdit ID="txtExpireDate" ClientInstanceName="txtExpireDate" runat="server">
                                        </dx:ASPxDateEdit>
                                    </td>
                                </tr>
                                <tr>
                                    <td colspan="2">
                                        <dx:ASPxButton ID="btnSendQuotationToCustomer" ClientInstanceName="btnSendQuotationToCustomer" runat="server" Text="Udsend tilbud på email" AutoPostBack="false">
                                            <ClientSideEvents Click="function(s, e) { SendQuotationToCustomer();}" />
                                        </dx:ASPxButton>
                                    </td>
                                </tr>
                            </table>
                        </dx:PanelContent>
                    </PanelCollection>
                </dx:ASPxPanel>
                
            </td>
        </tr>
    </table>

</asp:Content>
